<?php
require_once '../sub/init.php';
header("Cache-control: private");
require_once 'chkUadm.php';
if(!in_array('u_add',$QXARR))exit(noauth());
require_once ZEAI.'cache/config_up.php';
require_once ZEAI.'sub/zeai_up_func.php';

if($submitok=="import"){
	require_once ZEAI.'sub/excel_reader.php';
	$file = $_FILES['execl'];
	$file_name = $file['tmp_name'];
	$data = new Spreadsheet_Excel_Reader();
	$data->setOutputEncoding('UTF-8');
	$data->read(iconv("UTF-8","GB2312",$file_name));
	//$data->sheets[0]['numRows']为Excel行数
	//$data->sheets[0]['numCols']为Excel列数
	$echo = '';
	$total=$data->sheets[0]['numRows'];
	$n=0;$s=0;
	for($i=2;$i<=$total;$i++){
		$oldid = trimhtml(dataIO($data->sheets[0]['cells'][$i][1],'in',11));
		$old_vip1 = trimhtml(dataIO($data->sheets[0]['cells'][$i][2],'in',11));$old_vip1 = str_replace("无","",$old_vip1);
		$old_vip2 = trimhtml(dataIO($data->sheets[0]['cells'][$i][3],'in',11));
		
		$nickname = trimhtml(dataIO($data->sheets[0]['cells'][$i][8],'in',50));
		$truename = trimhtml(dataIO($data->sheets[0]['cells'][$i][9],'in',12));
		$photo_s  = trimhtml(dataIO($data->sheets[0]['cells'][$i][10],'in'));
		$sex      = trimhtml(dataIO($data->sheets[0]['cells'][$i][11],'in',2));if($sex=="女"){$sex=2;}else{$sex=1;}
		$mob      = trimhtml(dataIO($data->sheets[0]['cells'][$i][12],'in',11));
		$RZ       = '';
		if(!empty($mob)){
			if(ifmob($mob)){
				$row=$db->ROW(__TBL_USER__,"id","mob='$mob'");
				if($row){$s++;continue;}
				$RZ='mob';
			}else{
				$s++;continue;
			}
		}
		
		$weixin   = trimhtml(dataIO($data->sheets[0]['cells'][$i][13],'in',20));
		$birthday = trimhtml(dataIO($data->sheets[0]['cells'][$i][16],'in',20));if(!ifdate($birthday)){$birthday='0000-00-00';}
		$heigh    = intval(trimhtml(dataIO($data->sheets[0]['cells'][$i][17],'in',20)));
		$weigh    = intval(trimhtml(dataIO($data->sheets[0]['cells'][$i][18],'in',20)));
		$nation    = trimhtml(dataIO($data->sheets[0]['cells'][$i][20],'in',20));$nation = getNationId($nation);

		$edu   = trimhtml(dataIO($data->sheets[0]['cells'][$i][19],'in',100));
		switch($edu){case"初中":$edu=1;break;case"高中及以下":$edu=2;break;case"大专":$edu=3;break;case"大学本科":$edu=4;break;case"硕士":$edu=5;break;case"博士":$edu=6;break;default:$edu=0;break;}
		
		$love  = trimhtml(dataIO($data->sheets[0]['cells'][$i][23],'in',20));
		switch($love){
			case"未婚":$love=1;break;
			case"已婚":$love=2;break;
			case"离异":$love=3;break;
			case"丧偶":$love=4;break;
			case"保密":$love=5;break;
			default:$love=0;break;
		}
		
		$child  = trimhtml(dataIO($data->sheets[0]['cells'][$i][24],'in',20));
		switch($child){
			case"没有小孩":$child=1;break;
			case"有小孩跟随自己":$child=2;break;
			case"有孩子且偶尔会一起住":$child=3;break;
			default:$child=0;break;
		}
		
		$car  = trimhtml(dataIO($data->sheets[0]['cells'][$i][26],'in',20));
		switch($car){
			case"已买车":$car=1;break;
			case"未买车":$car=2;break;
			default:$car=0;break;
		}
		
		$house  = trimhtml(dataIO($data->sheets[0]['cells'][$i][27],'in',20));
		switch($house){
			case"已购房":$house=1;break;
			case"和家人同住":$house=7;break;
			case"婚后购房":$house=3;break;
			case"住在单位宿舍":$house=11;break;
			case"租房":$house=8;break;
			default:$house=0;break;
		}
		
		$pay  = trimhtml(dataIO($data->sheets[0]['cells'][$i][29],'in',20));
		switch($pay){
			case"3000元以下":$pay=3;break;
			case"3001元-5000元":$pay=4;break;
			case"5001-8000元":$pay=5;break;
			case"8001-12000元":$pay=6;break;
			case"12001-20000元":$pay=7;break;
			case"20001-50000元":$pay=8;break;
			case"50000元以上":$pay=9;break;
			default:$pay=0;break;
		}
		
		
		//户籍地区
		$area2title = trimhtml(dataIO($data->sheets[0]['cells'][$i][33],'in',100));
		$area2id    = areaidFn($area2title);
		
		//工作地区
		$areatitle = trimhtml(dataIO($data->sheets[0]['cells'][$i][34],'in',100));
		$areaid    = areaidFn($areatitle);
		
		
		$companyname  = trimhtml(dataIO($data->sheets[0]['cells'][$i][35],'in',20));
		
		/******************择偶要求*********************/
		$age  = trimhtml(dataIO($data->sheets[0]['cells'][$i][37],'in',20));
		$age = explode('-',$age);
		$mate_age1 = intval($age[0]);
		$mate_age2 = intval($age[1]);
		
		$heigh_  = trimhtml(dataIO($data->sheets[0]['cells'][$i][38],'in',20));
		$heigh_ = explode('-',$heigh_);
		$mate_heigh1 = intval($heigh_[0]);
		$mate_heigh2 = intval($heigh_[1]);
				
		$mate_pay  = trimhtml(dataIO($data->sheets[0]['cells'][$i][39],'in',20));
		switch($mate_pay){
			case"3000元以下":$mate_pay=3;break;
			case"3001元-5000元":$mate_pay=4;break;
			case"5001-8000元":$mate_pay=5;break;
			case"8001-12000元":$mate_pay=6;break;
			case"12001-20000元":$mate_pay=7;break;
			case"20001-50000元":$mate_pay=8;break;
			case"50000元以上":$mate_pay=9;break;
			default:$mate_pay='';break;
		}

		$mate_edu  = trimhtml(dataIO($data->sheets[0]['cells'][$i][40],'in',20));
		switch($mate_edu){case"初中":$mate_edu=1;break;case"高中及以下":$mate_edu=2;break;case"大专":$mate_edu=3;break;case"大学本科":$mate_edu=4;break;case"硕士":$mate_edu=5;break;case"博士":$mate_edu=6;break;default:$mate_edu='';break;}
		
		//择偶地区
		$mate_areatitle = trimhtml(dataIO($data->sheets[0]['cells'][$i][42],'in',100));
		$mate_areaid    = areaidFn($mate_areatitle);
		/******************择偶要求 结束*********************/
		
		
		
		$bz  = '原编号：'.$oldid;
		$bz .= (!empty($old_vip1))?'｜VIP：'.$old_vip1.'｜截止时间：'.$old_vip2:'';
		
		
		
		$crm_ubz = $bz;
		
		$regtime = ADDTIME;
		$endtime = ADDTIME;
		$refresh_time= ADDTIME;
		$regkind=12;
		$dataflag=1;
		$flag=1;
		$kind=intval($kind);
		$crm_fromclue=intval($crm_fromclue);
		$crm_ukind=intval($crm_ukind);
		//
		$db->query("INSERT INTO ".__TBL_USER__." (mob,sex,nickname,truename,birthday,heigh,weigh,love,areaid,areatitle,area2id,area2title,weixin,edu,RZ,aboutus,bz,crm_ubz,regtime,endtime,refresh_time,dataflag,flag,regkind,qq,email,kind,crm_ukind,crm_fromclue,nation,child,house,companyname) VALUES ('$mob','$sex','$nickname','$truename','$birthday','$heigh','$weigh','$love','$areaid','$areatitle','$area2id','$area2title','$weixin','$edu','$RZ','$aboutus','$bz','$crm_ubz','$regtime','$endtime','$refresh_time','$dataflag','$flag','$regkind','$qq','$email','$kind','$crm_ukind','$crm_fromclue','$nation','$child','$house','$companyname')");
		$uid = $db->insert_id();
		//下载头像
		$SQL = '';$p_s='';
		if(!empty($photo_s)){
			$url=$photo_s;
			$dbname = @wx_get_uinfo_logo($url,$uid);
			$_s     = setpath_s($dbname);
			//$newphoto_s = $_ZEAI['up2']."/".$_s;
			//if (ifpic($newphoto_s)){
				$p_s  =$_s;
				$SQL .= ",photo_s='$_s',photo_f=1";
			//}
		}
		
		
		//相册
		$photo = trimhtml(dataIO($data->sheets[0]['cells'][$i][45],'in'));//?imageView2/2/w/640/q/85
		if(!empty($photo)){
			$photo = explode(',',$photo);
			if (count($photo) >= 1 && is_array($photo)){
				foreach ($photo as $V) {
					$url = $V.'?imageView2/2/w/640/q/85';
					$dbname = wx_get_uinfo_logo($url,$uid,$dir='photo');
					$_s     = setpath_s($dbname);
					$db->query("INSERT INTO ".__TBL_PHOTO__." (uid,path_s,flag,addtime) VALUES ($uid,'$_s',1,".ADDTIME.")");
				}
			}
		}
		
		//
		$uname = 'excel'.$uid;
		$pwd = md5($mob);
		if(empty($nickname))$nickname='id'.$uid;
		$db->query("UPDATE ".__TBL_USER__." SET uname='$uname',pwd='$pwd'".$SQL." WHERE id=".$uid);
		shalou_init($uid);
		//
		$row_shalou=shalou_data($uid);
		$row_shalou['truename']=$truename;
		if(!empty($p_s))$row_shalou['photo_s']=$p_s;
		$new_row_shalou=encode_json($row_shalou);
		$db->query("UPDATE ".__TBL_SHALOU__." SET jsonstr='$new_row_shalou' WHERE uid=".$uid);
		//
		set_data_ed_bfb($uid);
		$n++;
	}
	if($n>0)AddLog('【Excel导入用户】->'.$n.'个');
	alert('成功导入：'.$n.'个，失败：'.$s.'个','back');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<style>
.box{min-width:1200px;padding:0}
.table {width:1000px;border:#F0F3F6 1px solid}
.table .tbodyT{font-size:18px;height:60px}
.table .tbodyT h6{color:#999;display:block}
</style>
<script src="<?php echo RES;?>www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/cache/udata.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/res/select4.js?<?php echo $_ZEAI['cache_str'];?>"></script>
</head>
<body>
<div class="box">
	<div class="boxL">
        <a href="u_add.php">用户人工录入</a>
        <a href="import.php">Excel表导入</a>
        <a href="import_weiyuan.php">微缘数据导入</a>
        <a href="import_cfy.php" class="ed">楚房云数据导入</a>
    </div>
	<div class="boxR">
        <form action="import_cfy.php?submitok=import" name="form_execl" method="post" enctype="multipart/form-data" onSubmit="zeai.msg('数据导入中，请匆关闭窗口...',{time:7200})">
        <table class="table" style="margin:15px 0 15px 20px">
            <tr>
                <td width="500" height="60" align="center"  class="tbodyT">
                <center>楚房云Excel表格数据导入
                <h6><font class="Cf00">此导入为第三方系统楚房云专享导入通道，新客户请点左侧《Excel表导入》</font></h6>
                </center>
                </td>
          </tr>
            <tr>
              <td class="border0 S14" style="padding:20px 0;background-color:#fff">
              <div style="width:390px;margin:0 auto;text-align:left">
              
              用户类型：<input type="radio" name="kind" value="1" id="kind_1" class="radioskin"><label for="kind_1" class="radioskin-label"><i class="i1"></i><b class="W120">线上用户</b></label>
        <input type="radio" name="kind" value="2" id="kind_2" class="radioskin" checked><label for="kind_2" class="radioskin-label"><i class="i1"></i><b class="W120">线下用户</b></label>
        <input type="radio" name="kind" value="3" id="kind_3" class="radioskin"><label for="kind_3" class="radioskin-label"><i class="i1"></i><b class="W120">均可(线上+线下)</b></label><br><br>

              
              线索来源：<script>zeai_cn__CreateFormItem('select','crm_fromclue','<?php echo $crm_fromclue; ?>','class="size2 W200"',crm_fromclue_ARR);</script>　<a href="crm_udata3.php" class="blue ">修改线索选项</a><br><br>
              客户分类：<script>zeai_cn__CreateFormItem('select','crm_ukind','<?php echo $crm_ukind; ?>','class="size2 W200"',crm_ukind_ARR);</script>　<a href="crm_udata3.php" class="blue">修改分类选项</a><br><br>
              上传表格：<input type="file" accept="application/vnd.ms-excel" name="execl" class="input size2 W300" /> 
              </div>
				<br><input type="submit" class="btn size3 " name="submit" value="开始导入" />
            </td>
            </tr>
            <tr><td align="left" style="font-size:14px;line-height:200%;padding:20px;background-color:#fff">
            注意事项：<br>
            1、导入的Execl表格请从楚房云后台导出下载，请勿私自改动数据结构<br>
            2、在导入过程中请勿关闭页面<br>
            3、如果遇到导入出错，请联系管理员<br>
            4、由于数据量太大，请确认PHP环境超时时间足够长，一般至少要10分钟，发送过程中请不要关闭窗口，耐心等待<br>
            5、有手机的新密码为手机号码，导入后推荐用ID+密码或手机+密码方式登录<br>
            6、如发现手机重复已存在将跳过进行下一条记录<br>
            7、如果提醒没有选中的文件，请用微软Excel打开数据另存为 Excel97-2003格式，如下图<br>
            <img src="images/excel_1.png">
            </td></tr>
        </table>
        </form>
    </div>
    <div class="clear"></div>
</div>
<?php
function getNationId($str){
	switch($str){
		case"汉族":$nationid=1;break;
		case"藏族":$nationid=2;break;
		case"朝鲜族":$nationid=3;break;
		case"蒙古族":$nationid=4;break;
		case"回族":$nationid=5;break;
		case"满族":$nationid=6;break;
		case"维吾尔族":$nationid=7;break;
		case"壮族":$nationid=8;break;
		case"彝族":$nationid=9;break;
		case"苗族":$nationid=10;break;
		case"侗族":$nationid=11;break;
		case"瑶族":$nationid=12;break;
		case"白族":$nationid=13;break;
		case"布依族":$nationid=14;break;
		case"傣族":$nationid=15;break;
		case"京族":$nationid=16;break;
		case"黎族":$nationid=17;break;
		case"羌族":$nationid=18;break;
		case"怒族":$nationid=19;break;
		case"佤族":$nationid=20;break;
		case"水族":$nationid=21;break;
		case"畲族":$nationid=22;break;
		case"土族":$nationid=23;break;
		case"阿昌族":$nationid=24;break;
		case"哈尼族":$nationid=25;break;
		case"高山族":$nationid=26;break;
		case"景颇族":$nationid=27;break;
		case"珞巴族":$nationid=28;break;
		case"锡伯族":$nationid=29;break;
		case"德昂(崩龙)族":$nationid=30;break;
		case"保安族":$nationid=31;break;
		case"基诺族":$nationid=32;break;
		case"门巴族":$nationid=33;break;
		case"毛南族":$nationid=34;break;
		case"赫哲族":$nationid=35;break;
		case"裕固族":$nationid=36;break;
		case"撒拉族":$nationid=37;break;
		case"独龙族":$nationid=38;break;
		case"普米族":$nationid=39;break;
		case"仫佬族":$nationid=40;break;
		case"仡佬族":$nationid=41;break;
		case"东乡族":$nationid=42;break;
		case"拉祜族":$nationid=43;break;
		case"土家族":$nationid=44;break;
		case"纳西族":$nationid=45;break;
		case"傈僳族":$nationid=46;break;
		case"布朗族":$nationid=47;break;
		case"哈萨克族":$nationid=48;break;
		case"达斡尔族":$nationid=49;break;
		case"鄂伦春族":$nationid=50;break;
		case"鄂温克族":$nationid=51;break;
		case"俄罗斯族":$nationid=52;break;
		case"塔塔尔族":$nationid=53;break;
		case"塔吉克族":$nationid=54;break;
		case"柯尔克孜族":$nationid=55;break;
		case"乌兹别克族":$nationid=56;break;
		default:$nationid=0;break;
	}
	return $nationid;
}
function areaidFn($areatitle) {
	global $db;
	$areaid = '';
	if(!empty($areatitle)){
		$areatitle = str_replace("省","",$areatitle);
		$areatitle = str_replace("市","",$areatitle);
		$A=explode(',',$areatitle);
		$AT1 = $A[0];
		$AT2 = $A[1];
		$AT3 = $A[2];
		$AT4 = $A[3];
		if (!empty($AT1)){
			$rowa = $db->ROW(__TBL_AREA1__,"id","title LIKE '%".$AT1."%'","num");
			if ($rowa){
				$a1 = intval($rowa[0]);	
				$areaid = $a1;
				if (!empty($AT2)){
					$rowa = $db->ROW(__TBL_AREA2__,"id","fid=".$a1." AND title LIKE '%".$AT2."%'","num");
					if ($rowa){
						$a2 = intval($rowa[0]);
						$areaid = $a1.','.$a2;
						if (!empty($AT3)){
							$rowa = $db->ROW(__TBL_AREA3__,"id","fid=".$a2." AND title LIKE '%".$AT3."%'","num");
							if ($rowa){
								$a3 = $rowa[0];
								$areaid = $a1.','.$a2.','.$a3;
								if (!empty($AT4)){
									$rowa = $db->ROW(__TBL_AREA4__,"id","fid=".$a3." AND title LIKE '%".$AT4."%'","num");
									if ($rowa){
										$a4 = $rowa[0];
										$areaid = $a1.','.$a2.','.$a3.','.$a4;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return $areaid;
}		
require_once 'bottomadm.php';?>